// Created by carolsail

import { ENUM_AUDIO_CLIP, ENUM_UI_TYPE } from "../Enum";
import { StaticInstance } from './../StaticInstance';
import AudioManager from "../manager/AudioManager";
import BaseLayer from "./Baselayer";
import DataManager from "../manager/DataManager";
import PoolManager from "../manager/PoolManager";
import ResourceManager from "../manager/ResourceManager";
import SdkManager from "../manager/SdkManager";
import ToastManager from "../manager/ToastManager";
import { privacyChannel } from "../Script/privacyChannel";
import bh_toast from "../Script/bh_toast";
import MenuLayer from "./MenuLayer";

const { ccclass, property } = cc._decorator;

@ccclass
export default class AddPowerLayer extends BaseLayer {

    panel: cc.Node = null
    btnClose: cc.Node = null
    btnUpdate: cc.Node = null
    videoTip: cc.Node = null

    onLoad() {
        this.panel = cc.find('style/panel', this.node)
        this.btnClose = cc.find('style/panel/buttons/btn_next', this.node)
        this.btnUpdate = cc.find('style/panel/buttons/btn_upgrade', this.node)
        this.videoTip = cc.find("style/panel/videotip", this.node)
        this.btnClose.on('click', this.onClickClose, this)
        this.btnUpdate.on('click', this.onClickGet, this)
        cc.find("style/panel/videotip/box", this.node).on("click", this.onClickBox, this)


    }
    show(): void {
        super.show()
        this.btnClose.active = false

        switch (privacyChannel.channel) {
            case "oppo":
            case "oppo1":
            case "vivo":
            case "vivo1":
            case "xm":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                    try {
                        privacyChannel.getChannel().showBanner()
                    } catch {

                    }
                }
                break
            case "ks":
                if (privacyChannel.getChannel().getCtrl("p2")) {

                    privacyChannel.getChannel().showInter(true)
                }
                break
        }
    }
    onClickBox() {
        console.error("点击按钮")
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        this.videoTip.getChildByName("mark").active = !this.videoTip.getChildByName("mark").active
        if (this.videoTip.getChildByName("mark").active) {
            this.btnClose.active = false
        } else {
            this.btnClose.active = true
        }
    }
    onClickClose() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.ADDPOWER, false)
        StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
    }

    onClickGet() {
        AudioManager.instance.playSound(ENUM_AUDIO_CLIP.CLICK)

        privacyChannel.getChannel().showVideo(() => {
            DataManager.instance.power += 10
            DataManager.instance.save()
            this.node.parent.getChildByName("MenuLayer").getComponent(MenuLayer).rendorPower()
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.ADDPOWER, false)
            StaticInstance.uiManager.toggle(ENUM_UI_TYPE.MENU)
        })

    }

    onDestroy() {
        this.btnClose.off('click', this.onClickClose, this)
        this.btnUpdate.off('click', this.onClickGet, this)
    }

    onEnable() {

    }

    onDisable() {

    }





}
